<?php

namespace api\modules\v1\controllers;

use api\controllers\OnAuthController;
use common\models\jinanhotlist\Jinanhotlist;
use Yii;

/**
 * 济南热榜
 *
 * This is the model class for table "{{%jinanhotlist}}".
 *
 * @property int $id 主键id
 * @property string $title 标题
 * @property string $image 图片
 * @property string $content 新闻内容
 * @property string $source 来源，新闻社
 * @property string $editor 编辑人
 * @property int $sort 排序
 * @property int $status 状态
 * @property int $fabulous 点赞状态:1=已点赞,2=未点赞
 * @property int $collection 收藏状态:1=已收藏,2=未收藏
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 */
class JinanhotlistController extends OnAuthController
{
    public $modelClass = JinanhotlistController::class;
    protected $authOptional = ['index','details','fabulous','collection'];
    /**
     * 首页面显示
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    //首页显示
    public function actionIndex()
    {
        $data = Jinanhotlist::find()->select('id,title,image')->orderBy(['id'=>SORT_DESC])->limit(50)->all();
        return $data;
    }
    //详情页查询展示
    public function actionDetails()
    {
        $id = Yii::$app->request->get('id', null);
        if (empty($id)){
            return '请求失败';
        }else{
            $data = Jinanhotlist::find()->where(['id'=>$id])->one();
            return $data;
        }
    }
    //点赞接口
    public function actionFabulous(){
        $id = Yii::$app->request->get('id', null);
        $fabulous = Yii::$app->request->get('fabulous');
        if (!empty($fabulous)&&!empty($id)){
            if($fabulous==2){
                Jinanhotlist::updateAll(['fabulous'=>'1'], ['id'=>$id]);
                return '点赞成功';
            }else{
                Jinanhotlist::updateAll(['fabulous'=>2],['fabulous'=>1,'id'=>$id]);
                return '取消点赞成功';
            }
        }else{
            return '请求失败';
        }
    }
    //收藏接口
    public function actionCollection(){
        $id = Yii::$app->request->get('id', null);
        $collection = Yii::$app->request->get('collection');
        if (!empty($collection)&&!empty($id)){
            if($collection==2){
                Jinanhotlist::updateAll(['collection'=>'1'], ['id'=>$id]);
                return '收藏成功';
            }else{
                Jinanhotlist::updateAll(['collection'=>2],['fabulous'=>1,'id'=>$id]);
                return '取消收藏成功';
            }
        }else{
            return '请求失败';
        }
    }
}
